package com.example.uniapp.activity.main;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;

import com.example.uniapp.R;
import com.example.uniapp.dao.UserDao;
import com.example.uniapp.utils.FileImgUtils;

public class RegisterActivity extends AppCompatActivity {
    /**
     * 用户注册界面
     */
    private ActivityResultLauncher<String> getContentLaucher;
    private Uri uri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        //返回功能
        Toolbar toolbar = findViewById(R.id.register_back);
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish(); //返回
            }
        });

        //获取默认头像
        Drawable defaultDrawable = ContextCompat.getDrawable(this, R.drawable.default_avator);

        //用户注册信息
        ImageView avatorView = findViewById(R.id.register_avator);
        EditText accountText = findViewById(R.id.register_account);
        EditText pwdText = findViewById(R.id.register_pwd);
        EditText repwdText = findViewById(R.id.register_repwd);

        //初始化文件选择器
        getContentLaucher=registerForActivityResult(new ActivityResultContracts.GetContent(), new ActivityResultCallback<Uri>() {
            @Override
            public void onActivityResult(Uri result) {
                if(result!=null) {
                    avatorView.setImageURI(result);
                    uri=result;
                }else {
                    Toast.makeText(RegisterActivity.this, "未选择头像", Toast.LENGTH_SHORT).show();
                }
            }
        });

        //注册按钮监听
        Button registerButton = findViewById(R.id.register_register);
        registerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //获取各参数
                String account = accountText.getText().toString();
                String pwd = pwdText.getText().toString();
                String repwd = repwdText.getText().toString();

                //判断此人有没有选择头像
                Drawable avatorDrawable = avatorView.getDrawable(); //获取图片
                Bitmap avatorBitmap = ((BitmapDrawable) avatorDrawable).getBitmap();
                Bitmap defaultBitmap = ((BitmapDrawable) defaultDrawable).getBitmap();

                if(avatorBitmap.sameAs(defaultBitmap)) {
                    //如果二者相等，代表未添加头像
                    Toast.makeText(RegisterActivity.this, "请点击图片以添加头像", Toast.LENGTH_SHORT).show();
                }else if(account.isEmpty()) {
                    Toast.makeText(RegisterActivity.this, "请输入用户账号", Toast.LENGTH_SHORT).show();
                }else if(pwd.isEmpty()) {
                    Toast.makeText(RegisterActivity.this, "请输入用户密码", Toast.LENGTH_SHORT).show();
                }else if(repwd.isEmpty()) {
                    Toast.makeText(RegisterActivity.this, "请再次输入用户密码", Toast.LENGTH_SHORT).show();
                }else if(!repwd.equals(pwd)) {
                    Toast.makeText(RegisterActivity.this, "两次密码输入不一致", Toast.LENGTH_SHORT).show();
                }else {
                    //运行到这，代表可以保存数据到数据库
                    String avatorPath = FileImgUtils.getImgName(); //获取图片存储路径
                    FileImgUtils.saveImageUriToFileImg(uri, RegisterActivity.this, avatorPath); //保存图片
                    int result = UserDao.saveUser(account, pwd, avatorPath); //保存到数据库
                    if(result == 1) {
                        Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();

                        //注册结束后返回到登录界面
                        finish();
                    }else {
                        Toast.makeText(RegisterActivity.this, "注册失败,用户名已被占用", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });

        //点击头像上传头像
        avatorView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openGallery(v);
            }
        });
    }

    private void openGallery(View v) {
        //打开文件选择器
        getContentLaucher.launch("image/*");
    }
}